varying vec3 pos,l,n,v,h;
varying vec2 texCoord;
varying vec4 posit;
uniform sampler2D envMap; 
uniform sampler2D leaf;
uniform sampler2D shadowMap;  
varying bool reverse;
uniform vec3 eyePos;
 
float module(vec3 a, vec3 b) {
    vec3 c = a - b;
    return sqrt(c.x*c.x + c.y*c.y + c.z*c.z);
}   
void main(void) {
   float fr = module(eyePos,pos);
   float start = 0.0;
   float end = 1000.0;
   float summar = min(max( dot(normalize(l), vec3(0.0,0.0,1.0)), 0.1 ),0.5);
   if(fr > end) { 
   		gl_FragColor = vec4(summar);
   		return;
   } 
   float  r;
   r =  max(0.2,dot(l, n));
   if(reverse) r =  max(0.2,dot(l, n) -0.2);
   vec4 color = texture2D(leaf,texCoord);
   float transparency = color.w;
   gl_FragColor = color*r;
   gl_FragColor.w = transparency;
      if(pos.z > 0.0 && fr > start) {
    fr = 1.0/(1.0 - (fr/end));
        vec3 shad_coor = (posit.xyz / posit.w) * 0.5 + 0.5;
    float s = texture2D(shadowMap,shad_coor.xy).x+0.001;
    if(shad_coor.z >= s) gl_FragColor.xyz /= 2.0;
    gl_FragColor.x = summar + (gl_FragColor.x - summar)/fr;
    gl_FragColor.y = summar + (gl_FragColor.y - summar)/fr;
    gl_FragColor.z = summar + (gl_FragColor.z - summar)/fr;
    }

}